package tree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
//https://leetcode.cn/leetbook/read/illustration-of-algorithm/7f4ucc/
//一棵圣诞树记作根节点为 root 的二叉树，节点值为该位置装饰彩灯的颜色编号。请按照从左到右的顺序返回每一层彩灯编号，每一层的结果记录于一行。
public class 彩灯装饰记录2 {
      public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }
    class Solution {
        public List<List<Integer>> decorateRecord(TreeNode root) {
//            if(root == null) return null;
            Queue<TreeNode> queue = new LinkedList<>();
            List<List<Integer>> res = new ArrayList<>();
            if(root != null) queue.add(root);
            while (!queue.isEmpty()){
                List<Integer> tmp = new ArrayList<>();
                for(int i=queue.size(); i>0; i--){
                    TreeNode node = queue.poll();
                    tmp.add(node.val);
                    if(node.left != null) queue.add(node.left);
                    if(node.right!= null) queue.add(node.right);
                }
                res.add(tmp);
            }
            return res;
        }
    }
}
